package com.bysj.lms.controller;


import com.bysj.lms.common.ResultModel;
import com.bysj.lms.pojo.entity.User;
import com.bysj.lms.pojo.vo.UserVo;
import com.bysj.lms.service.IRoleService;
import com.bysj.lms.service.IUserService;
import com.bysj.lms.utils.EncryptUtil;
import com.bysj.lms.utils.SaltUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.regex.Pattern;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-02-04
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    IUserService userService;
    @Autowired
    IRoleService roleService;

    @RequiresRoles(value = "2")
    @ApiOperation(value = "新增用户")
    @PostMapping("/insert")
    public ResultModel insert(@RequestBody UserVo userVo){
        boolean add = userService.add(userVo);
        return add?ResultModel.success("新增成功"):ResultModel.failure("新增失败");
    }


    @ApiOperation(value = "查询用户",notes = "username为空得到所有用户分页查询信息，不为空根据username模糊查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页"),
            @ApiImplicitParam(name = "count",value = "页的大小"),
            @ApiImplicitParam(name = "name",value = "用户姓名")
    })
    @GetMapping("/select")
    @RequiresRoles("2")
    public ResultModel find(String name,Integer page,Integer count){

        PageInfo pageInfo = userService.findByPage(name,page,count);
        return ResultModel.success("用户分页查询",pageInfo);
    }

    @ApiOperation("用户详情")
    @GetMapping("/userInfo")
    @RequiresPermissions("lms:user:userInfo")
    public ResultModel getUserInfo(String username){
        UserVo user = userService.getByUsername(username);
        return ResultModel.success("用户详情",user);
    }

    @ApiOperation(value = "根据id更改用户信息")
    @PutMapping("/update")
    @RequiresPermissions("lms:user:update")
    public ResultModel update(@RequestBody UserVo userVo){
        boolean edit = userService.edit(userVo);
        if(edit) return ResultModel.success("修改成功");
        return ResultModel.failure("修改失败");

    }

    @ApiOperation(value = "修改密码")
    @PutMapping("/changePassword")
    public ResultModel changePassword(String username,String oldPassword,String newPassword){
        return userService.changePwd(username, oldPassword, newPassword);
    }

    @ApiOperation(value = "根据id删除用户",notes = "逻辑删除")
    @DeleteMapping("/delete")
    @RequiresRoles("2")
    public ResultModel delete(String id){
        boolean delete = userService.delete(id);
        if(delete){
            return ResultModel.success("删除成功");
        }
        return ResultModel.failure("删除失败");
    }

    @GetMapping("")
    public User getModel(){
        return new User();
    }
}

